﻿using Approval.API.Applcation.Command.RLZRelationCommand;
using Approval.Domain;
using Approval.Domain.Common;
using Approval.Infrastructure.Impl;
using Approval.Infrastructure.Interface;
using MediatR;
using Newtonsoft.Json;
using PublicCommon;

namespace Approval.API.Applcation.CommandHandler.RLZRelationCommandHandler
{
    public class RLZRelationApprovalCommandHandler : IRequestHandler<RLZRelationApprovalCommand, Audit>
    {
        private readonly IRLZRelationRepository rLZRelationRepository;
        private readonly Cross_services_Helper cross_services_helper;

        public RLZRelationApprovalCommandHandler(IRLZRelationRepository rLZRelationRepository, Cross_services_Helper cross_services_helper)
        {
            this.rLZRelationRepository = rLZRelationRepository;
            this.cross_services_helper = cross_services_helper;
        }

        public async Task<Audit> Handle(RLZRelationApprovalCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var info = rLZRelationRepository.All().Result.Where(x => x.RLZId == request.RLZId && x.ApprovalId == request.UserId).FirstOrDefault();
                //审批时间
                info.CreateDate = DateTime.Now;
                //审批备注
                info.ApprovalDesc = request.ApprovalDesc;
                //同意
                if (request.Identifier == 1)
                {
                    info.ApprovalState = 2;
                    await rLZRelationRepository.Update(info);
                    var hctapprocal = new RLZRelation()
                    {
                        RLZId = request.RLZId,
                        ApprovalId = request.TransferId,
                        ApprovalState = 1,
                        ApprovalName = request.TransferName,
                        ApprovalDesc = null,
                    };
                    var res = await rLZRelationRepository.Create(hctapprocal);
                    var audit = new Audit()
                    {
                        CreateDate = DateTime.Now,
                        CreateBy = request.TransferName,
                        CreateById = request.TransferId,
                        AuditState = 1,
                        IsDel = false
                    };
                    return audit;
                }
                //拒绝
                if (request.Identifier == 2)
                {
                    info.ApprovalState = 3;
                    await rLZRelationRepository.Update(info);
                    var audit = new Audit()
                    {
                        CreateDate = DateTime.Now,
                        CreateBy = request.UserName,
                        CreateById = request.UserId,
                        AuditState = 1,
                        IsDel = false
                    };
                    //修改审批状态
                    var Rlz = await cross_services_helper.Get($"http://10.31.60.9:8000/employeeread/api/Personnel/GetPersonnelByid?id={request.RLZId}");
                    var Rlzs = JsonConvert.DeserializeObject<RLZDto>(Rlz);
                    var RlzList = new RLzState();
                    RlzList.id = Rlzs.Id;
                    RlzList.state = 3;
                    Rlzs.ApprovedBy = "小明";
                    var res = await cross_services_helper.Put($"http://10.31.60.9:8000/employee/api/Personnel/UpdateByidStatePersonnels", RlzList);




                    return audit;
                }
                //通过并终止转批
                if (request.Identifier == 3)
                {
                    info.ApprovalState = 2;
                    await rLZRelationRepository.Update(info);
                    var audit = new Audit()
                    {
                        CreateDate = DateTime.Now,
                        CreateBy = request.UserName,
                        CreateById = request.UserId,
                        AuditState = 1,
                        IsDel = false
                    };
                    //修改审批状态
                    var Rlz = await cross_services_helper.Get($"http://10.31.60.9:8000/employeeread/api/Personnel/GetPersonnelByid?id={request.RLZId}");
                    var Rlzs = JsonConvert.DeserializeObject<RLZDto>(Rlz);
                    var RlzList = new RLzState();
                    RlzList.id = Rlzs.Id;
                    Rlzs.ApprovedBy = "小明";
                    RlzList.state = 2;
                    var res = await cross_services_helper.Put($"http://10.31.60.9:8000/employee/api/Personnel/UpdateByidStatePersonnels", RlzList);

                    //修改人员工作状态
                    var Emp = await cross_services_helper.Get($"http://10.31.60.9:8000/employeeread/api/Personnel/GetStateArrayByid?id={request.RLZId}");
                    var Emps = JsonConvert.DeserializeObject<EmpState>(Emp);
                    var EmpList = new EmpState();
                    EmpList.id = Emps.id;
                    EmpList.state =Emps.state+1;
                    var EmpInfo = await cross_services_helper.Put($"http://10.31.60.9:8000/employee/api/Employee/UpdateEmployeeByid", EmpList);

                    return audit;
                }


                return info;
            }
            catch (Exception ex)
            {
                return null;
                throw;
            }
        }
    }
}
